package com.recipedb.recipedb.init;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Component
public class DefaultFileInitializer implements CommandLineRunner {

    @Value("${upload.base-path:uploads}")
    private String basePath;

    @Override
    public void run(String... args) throws Exception {
        Path defaultAvatar = Paths.get(basePath, "system/default_avatar.jpg");
        Files.createDirectories(defaultAvatar.getParent());
        if (!Files.exists(defaultAvatar)) {
            try (InputStream is = getClass().getResourceAsStream("/static/default_avatar.jpg")) {
                if (is != null) Files.copy(is, defaultAvatar);
            }
        }
    }
}
